using System;
using System.Collections.Generic;
using System.Linq;
using VoiceAd.Models;

namespace VoiceAd.DAL
{
	public class AudioCampRepository : IAudioCampRepository, IDisposable
	{
		private dbVoiceAdDataContext context = new dbVoiceAdDataContext();

		public AudioCampRepository(dbVoiceAdDataContext db)
		{
			this.context = db;
		}

		public IEnumerable<tblAudioCamp>GetListAudioCamps()
		{
			return context.tblAudioCamps.ToList();
		}

        public IEnumerable<tblAudioCamp> GetListAudioCampsbyCampId(int id)
        {
            return context.tblAudioCamps.Where(x => x.CampaignId == id).ToList();
        }

		public tblAudioCamp GetAudioCampById(int id)
		{
			return context.tblAudioCamps.Where(x => x.Id == id).FirstOrDefault();
		}

		public void InsAudioCamp(tblAudioCamp tblaudiocamp)
		{
			context.tblAudioCamps.InsertOnSubmit(tblaudiocamp);
			context.SubmitChanges();
		}

		public void UpdAudioCamp(tblAudioCamp tblaudiocamp)
		{
			tblAudioCamp obj =  context.tblAudioCamps.Where(x => x.Id == tblaudiocamp.Id).First();
			obj.CampaignId = tblaudiocamp.CampaignId;
			obj.UrlAudio = tblaudiocamp.UrlAudio;
			context.SubmitChanges();
		}

		public void DelAudioCamp(int id)
		{
			tblAudioCamp obj =  context.tblAudioCamps.Single(x => x.Id == id);
			context.tblAudioCamps.DeleteOnSubmit(obj);
			context.SubmitChanges();
		}

		private bool disposed = false;

		protected virtual void Dispose(bool disposing)
		{
			if (!this.disposed)
			{
				if (disposing)
				{
					context.Dispose();
				}
			}
			this.disposed = true;
		}

		public void Dispose()
		{
			Dispose(true);
			GC.SuppressFinalize(this);
		}
	}
}
